#include "SQLAssignItem.h"
#include "../../visitor/SQLASTVisitor.h"

SQLAssignItem::SQLAssignItem()
{
}

// BOOL_ptr equals(Object_ptr  o) {
//     if (this == o) {
//         return BOOL::TRUE;
//     }
//     if (o == NULL || getClass() != o->getClass()) {
//         return BOOL::FALSE;
//     }

//     SQLAssignItem* that = (SQLAssignItem*) o;

//     if (target != NULL ? !target->equals(that->target) : that->target != NULL) {
//         return BOOL::FALSE;
//     }
//     return value != NULL ? value->equals(that->value) : that->value == NULL;
// }

// int hashCode() {
//     int result = target != NULL ? target->hashCode() : 0;
//     result = 31 * result + (value != NULL ? value->hashCode() : 0);
//     return result;
// }

SQLAssignItem::SQLAssignItem(SQLExpr_ptr target, SQLExpr_ptr value)
{
  setTarget(target);
  setValue(value);
}

SQLObject_ptr SQLAssignItem::clone()
{
  SQLObject_ptr clone_tmp = SQLAssignItem_ptr(new SQLAssignItem());
  SQLAssignItem_ptr x = std::dynamic_pointer_cast<SQLAssignItem>(clone_tmp);
  // SQLAssignItem *x = new SQLAssignItem();
  if (target != NULL)
  {
    x->setTarget(std::dynamic_pointer_cast<SQLExpr>(target->clone()));
  }
  if (value != NULL)
  {
    x->setValue(std::dynamic_pointer_cast<SQLExpr>(value->clone()));
  }
  return x;
}

SQLObject_list_ptr  SQLAssignItem::getChildren()
{
  return NULL;
}

SQLExpr_ptr SQLAssignItem::getTarget()
{
  return target;
}

void SQLAssignItem::setTarget(SQLExpr_ptr x)
{
  if (x != NULL)
  {
    x->setParent(SharedObject(SQLAssignItem));
  }
  this->target = x;
}

SQLExpr_ptr SQLAssignItem::getValue()
{
  return value;
}

void SQLAssignItem::setValue(SQLExpr_ptr x)
{
  if (x != NULL)
  {
    x->setParent(SharedObject(SQLAssignItem));
  }
  this->value = x;
}

void SQLAssignItem::accept0(SQLASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SQLAssignItem)))
  {
    acceptChild(visitor, this->target);
    acceptChild(visitor, this->value);
  }
  visitor->endVisit(SharedObject(SQLAssignItem));
}

BOOL_ptr SQLAssignItem::replace(SQLExpr_ptr expr, SQLExpr_ptr target)
{
  if (this->target == expr)
  {
    setTarget(target);
    return BOOL::TRUE;
  }

  if (this->value == expr)
  {
    setValue(target);
    return BOOL::TRUE;
  }
  return BOOL::FALSE;
}
